package com.qxzn.app.personalcenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.RobotDeviceListBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.activity.QrScanActivity;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.personalcenter.adapter.RobotDeviceListAdapter;
import com.qxzn.app.util.AppUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 已绑定的机器人设备列表
 */
public class RobotDeviceListActivity extends BaseActivity {

    @BindView(R2.id.lrv_robot_list)
    LRecyclerView lrvRobotList;
    @BindView(R2.id.view_empty)
    View viewEmpty;
    private StringBuilder promptInfo;
    private LRecyclerViewAdapter recyclerViewAdapter;
    private List<RobotDeviceListBean.DataBean> dataList = new ArrayList();
    private int page = 1;
    private int pageSize = 10;
    private RobotDeviceListAdapter adapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_robot_device_list;
    }

    @Override
    protected void initView() {
        adapter = new RobotDeviceListAdapter(this, dataList);
        recyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                AppUtil.showLog("点击位置：" + position);
                startActivity(RobotInfoActivity.class);
            }
        });
        lrvRobotList.setLayoutManager(new LinearLayoutManager(this));
        lrvRobotList.setEmptyView(viewEmpty);
        lrvRobotList.setAdapter(recyclerViewAdapter);
        lrvRobotList.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppUtil.showLog("setOnRefreshListener");
                page = 1;
                getData();
            }
        });
        lrvRobotList.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                AppUtil.showLog("setOnLoadMoreListener");
                page++;
                getData();
            }
        });
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("已绑定的机器人");
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.logo);
        iv_right.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        lrvRobotList.refresh();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.iv_right) {
            startActivityForResult(new Intent(this, QrScanActivity.class), 1);
        }
    }

    private void getData() {
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                lrvRobotList.refreshComplete(pageSize);
//            }
//        }, 1000);
//        for (int i = (page - 1) * pageSize; i < page * pageSize; i++) {
//            dataList.add(new RobotDeviceListBean.DataBean());
//        }
//        recyclerViewAdapter.notifyDataSetChanged();
//        recyclerViewAdapter.notifyItemRangeChanged((page - 1) * pageSize, pageSize);

        Call<RobotDeviceListBean> call = HttpRequest.getService().getRobotDeviceList();
        call.enqueue(new Callback<RobotDeviceListBean>() {
            @Override
            public void onResponse(Call<RobotDeviceListBean> call, Response<RobotDeviceListBean> response) {
                lrvRobotList.refreshComplete(pageSize);
                RobotDeviceListBean bean = response.body();
                if (bean != null) {
                    List<RobotDeviceListBean.DataBean> deviceList = bean.getData();
                    if (deviceList != null) {
                        if (page == 1) {
                            dataList.clear();
                        }
                        dataList.addAll(deviceList);
                        adapter.notifyDataSetChanged();
                        recyclerViewAdapter.notifyDataSetChanged();
                        return;
                    }
                }
                if (bean != null && bean.getCode() != 200 && !TextUtils.isEmpty(bean.getMsg())) {
                    AppUtil.showToast(RobotDeviceListActivity.this, bean.getMsg());
                } else {
                    AppUtil.showToast(RobotDeviceListActivity.this, "网络错误，请稍后再试！");
                }
            }

            @Override
            public void onFailure(Call<RobotDeviceListBean> call, Throwable t) {
                AppUtil.showToast(RobotDeviceListActivity.this, "网络错误，请稍后再试！");
            }
        });
    }

    private void toAddDevice(){
        Intent intent = new Intent(this, AddRobotActivity.class);
        intent.putExtra("prompt_info", getString(R.string.guide_add_robot1));
        intent.putExtra("btn_text", "已开机");
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {
                String scan_result = data.getStringExtra("scan_result");
                if (!TextUtils.isEmpty(scan_result)) {
                    promptInfo = new StringBuilder();
                    promptInfo.append("嘻嘻！小π");
                    promptInfo.append(scan_result);
                    promptInfo.append("来啦\n");
                    promptInfo.append("主人是你吗！");
                    Intent intent = new Intent(this, AddRobotActivity.class);
                    intent.putExtra("prompt_info", promptInfo.toString());
                    intent.putExtra("btn_text", "立即绑定");
                    intent.putExtra("deviceId", scan_result);
                    startActivity(intent);
                }
            }
        }
    }

    @OnClick(R2.id.view_empty)
    public void onViewClicked() {
        lrvRobotList.refresh();
    }
}
